package org.vectrics.recruiter.struts.tag.employee;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.ecs.html.A;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;

import com.hexacta.hrs.services.employee.impl.EmploymentConditionDTO;

/**
 * Tag que muestra los datos historicos del empleado.
 * 
 * @author pgoette
 */
public class EmploymentConditionHistoryTag extends PageListTag {

    private static final long serialVersionUID = 4247431979714424201L;

    /** Id de persona. */
    private String personId;

    /** Id de la compania. */
    private String companyId;

    private String viewOptions;

    private Employee employee;

    public EmploymentConditionHistoryTag() {
        super();
        setWidth("100%");
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(final String personId) {
        this.personId = personId;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(final String companyId) {
        this.companyId = companyId;
    }

    @Override
    protected String getDescription() {
        return null;
    }

    @Override
    protected String getPageListLink() {
        return null;
    }

    public String getViewOptions() {
        return viewOptions;
    }

    public void setViewOptions(final String viewOptions) {
        this.viewOptions = viewOptions;
    }

    /**
     * Crea el PagetList de employment Condition Historys llamando al servicio
     * de empleado.
     */
    @Override
    protected PageList createPageList() {
        // Obtengo el empleado.
        employee = CoreServiceLocator.getCompanyService().findEmployee(Long.parseLong(personId),
                Long.parseLong(companyId));

        Set<EmploymentConditionDTO> employmentConditionHistorys = CoreServiceLocator.getEmployeeService()
                .getEmploymentConditionHistory(employee);

        PageList pageList = null;

        if (employmentConditionHistorys != null) {
            pageList = new EmploymentConditionHistoryPageList(new ArrayList<EmploymentConditionDTO>(
                    employmentConditionHistorys));
        } else {
            pageList = new EmploymentConditionHistoryPageList(new ArrayList<EmploymentConditionDTO>());
        }
        return pageList;
    }

    @Override
    protected void showList(final String description, final Cell mainCell, final List elements, final Form form) {
        BorderTable table = new BorderTable();
        addHeaderRow(table);
        if (elements != null) {
            for (Object employmentConditionDTO : elements) {
                addEmploymentConditionRow(table, (EmploymentConditionDTO) employmentConditionDTO);
            }
        }
        mainCell.addElement(table);
    }

    private void addEmploymentConditionRow(final Table table, final EmploymentConditionDTO employmentCondition) {

        Row row = table.addRow();
        row.setDefaultCellFontClass("subFont");
        // Effective Date
        Cell cell = row.addCell();
        cell.setVAlign("center");
        SimpleDateFormat dateFormat = DateUtil.createDateFormat(DateUtil.getFormatoFecha());
        cell.addElement(TextHelper.getFont("subFont", dateFormat.format(employmentCondition.getEffectiveDate())));

        // Condition
        cell = row.addCell();
        cell.setVAlign("center");
        if (employmentCondition.getCareerRole() != null) {
            cell.addElement(TextHelper.getFont("subFont", employmentCondition.getCareerRole().getCondition()
                    .getDescription()));
        } else {
            cell.addElement("  ");
        }

        // Career Role
        cell = row.addCell();
        cell.setVAlign("center");
        if (employmentCondition.getCareerRole() != null) {
            cell.addElement(TextHelper.getFont("subFont", employmentCondition.getCareerRole().getCareerRole()
                    .getDescription()));
        } else {
            cell.addElement(" ");
        }

        // Career Role Position
        cell = row.addCell();
        cell.setDefaultFontClass("subFont");
        cell.setVAlign("center");
        if (employmentCondition.getCareerRole() != null) {
            cell.addElement(TextHelper.getFont("subFont", employmentCondition.getCareerRole().getCareerRolePosition()
                    .toString()));
        } else {
            cell.addElement(" ");
        }

        // Hours Per Day
        cell = row.addCell();
        cell.setVAlign("center");
        if (employmentCondition.getAgreementContidion() != null) {
            cell.addElement(TextHelper.getFont("subFont", employmentCondition.getAgreementContidion().getHoursPerDay()
                    .toString()));
        } else {
            cell.addElement(" ");
        }

        // Assignable
        cell = row.addCell();
        cell.setVAlign("center");
        if (employmentCondition.getAssignabilityStatus() != null) {
            cell.addElement(TextHelper.getFont("subFont",
                    String.valueOf(employmentCondition.getAssignabilityStatus().isAvailable())));
        } else {
            cell.addElement(" ");
        }

        // Option
        if (new Boolean(getViewOptions())) {
            cell = row.addCell();
            cell.setDefaultFontClass("subFont");
            cell.setVAlign("center");
            cell.addElement(generarOpciones(employmentCondition));
        }

    }

    private Table generarOpciones(final EmploymentConditionDTO employmentCondition) {
        Table options = new Table();
        Row row = options.addRow();
        row.setDefaultCellFontClass("subFont");

        Cell cell = row.addCell();

        A editLink = new A();
        editLink.setClass("subLink");
        editLink.addElement(this.getPhrase("prompt.history.edit"));
        editLink.setHref(getContextPath() + "/admin/user/employee/edit.do?personId=" + employee.getPerson().getId()
                + "&companyId=" + employee.getCompany().getId() + "&effectiveDate="
                + employmentCondition.getEffectiveDate().getTime());
        cell.addElement(editLink);

        cell = row.addCell();
        cell.addElement(" &nbsp; ");

        if (!employee.getEmploymentConditionHistory().isLast()) {

            cell = row.addCell();
            A removeLink = new A();
            removeLink.setClass("subLink");
            removeLink.addElement(this.getPhrase("prompt.history.remove"));
            removeLink.setHref(getContextPath() + "/admin/user/employee/remove.do?personId="
                    + employee.getPerson().getId() + "&companyId=" + employee.getCompany().getId() + "&effectiveDate="
                    + employmentCondition.getEffectiveDate().getTime());

            Object[] parameter = { employee.getPerson().getFullName(),
                    DateUtil.getFechaFormateada(employmentCondition.getEffectiveDate()) };

            removeLink.setOnClick(" return (confirm('"
                    + this.getPhrase("prompt.history.remove.confirm.delete", parameter) + "'));");

            cell.addElement(removeLink);
        }

        return options;
    }

    private void addHeaderRow(final Table table) {
        Row row = table.addRow();
        row.setDefaultBgImage(getContextPath() + "/site-graphics/tbl_hdr.gif");
        row.setDefaultCellFontClass("tableHdrFont");

        Cell cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.effective.day")));

        cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.condition")));

        cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.career.role")));

        cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.position")));

        cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.hours")));

        cell = row.addCell();
        cell.setVAlign("center");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.assignable")));

        // Option
        if (new Boolean(getViewOptions())) {
            cell = row.addCell();
            cell.setVAlign("center");
            cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.options")));
        }
    }

}
